package com.sondon.mayi.activity;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemSelect;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.Area;
import com.sondon.mayi.entity.AreasEntity;
import com.sondon.mayi.entity.RespondBaseEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : ChooseRegionActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月10日 上午12:38:19
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 修改地区 }
 *
 */
@EActivity(R.layout.choose_region)
public class ChooseRegionActivity extends BaseActivity {
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	Spinner spin_province;
	@ViewById
	Spinner spin_city;
	@ViewById
	Spinner spin_district;
	@ViewById
	Button region_btn_ok;
	
	private ArrayAdapter<String> spin_adapter;//Spinner适配器
	
	private List<Area> provincelist =new ArrayList<Area>();
	private List<Area> citylist =new ArrayList<Area>();
	private List<Area> districtlist =new ArrayList<Area>();
	private List<Area> streetlist =new ArrayList<Area>();
	
	@AfterViews
	public void initView(){
		topBar.setTopBarClickListener(new TopBarClickListener() {
			
			@Override
			public void rightBtnClick() {
				
			}
			
			@Override
			public void leftBtnClick() {
					onBackPressed();
			}
		});
		
		//获取省信息  不填写area_id
		spinInitData("", spin_province,provincelist);
	}
	
	
 	@Click({R.id.region_btn_ok})
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.region_btn_ok:
	 		String provinceID=provincelist.get(spin_province.getSelectedItemPosition()).getArea_id();
			String cityID=citylist.get(spin_city.getSelectedItemPosition()).getArea_id();
			String districtID=districtlist.get(spin_district.getSelectedItemPosition()).getArea_id();
			String provinceName=provincelist.get(spin_province.getSelectedItemPosition()).getArea_name();
			String cityName=citylist.get(spin_city.getSelectedItemPosition()).getArea_name();
			String districtName=districtlist.get(spin_district.getSelectedItemPosition()).getArea_name();
			myprefs.region().put(provinceID+"#"+provinceName+"#"+cityID+"#"+cityName+"#"+districtID+"#"+districtName);
			
			
			this.finish();
			
			if(myprefs.is_frist().get()){
//				Bundle bundle=new Bundle();
//				bundle.putInt("position", 0);
//				openActivity(MainTabActivity_.class,bundle);
				myprefs.position().put(0);
				openActivity(MainTabActivity_.class);
			}
			
			
			
//			regionInfo();//修改我的信息   暂时没用
			break;

		default:
			break;
		}
	}
 	
 	
 	//修改个人信息
 	public void regionInfo(){
 		String provinceID=provincelist.get(spin_province.getSelectedItemPosition()).getArea_id();
		String cityID=citylist.get(spin_city.getSelectedItemPosition()).getArea_id();
		String districtID=districtlist.get(spin_district.getSelectedItemPosition()).getArea_id();
		
 		RequestParams params=new RequestParams();
 		params.put("province", provinceID);
 		params.put("city", cityID);
 		params.put("area", districtID);
 		params.put("true_name", "");
 		params.put("qq", "");
 		AsyncHttpUtil.post(URLConfigs.ChangeMyInfo_ukey+myprefs.Ukey().get(), params, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String json) {
				LogUtil.e(TAG, "ChangeMyInfo  json :"+json);
				if(200==status){
					RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
					if(entity.isStatus()){
						onBackPressed();
					}else{
						ToastUtils.show(context, entity.getMsg());
					}
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
				ToastUtils.show(context, R.string.network_unavailable);
			}
		});
 	}
 	
 	
 	/*******************************spinner 选择监听事件  start********************************/
	//省
	@ItemSelect({R.id.spin_province})
	public void onProvinceSelect(boolean selected, int position){
		LogUtil.e(TAG, "addAdress select position :"+position+"   selected :"+selected);
		if(selected){
			String area_id=provincelist.get(position).getArea_id();
			spinInitData(area_id, spin_city, citylist);
		}
	}
	//市
	@ItemSelect({R.id.spin_city})
	public void onCitySelect(boolean selected, int position){
		LogUtil.e(TAG, "addAdress select position :"+position+"   selected :"+selected);
		if(selected){
			String area_id=citylist.get(position).getArea_id();
			spinInitData(area_id, spin_district, districtlist);
		}
	}
	//县/区
	@ItemSelect({R.id.spin_district})
	public void onDistrictSelect(boolean selected, int position){
		LogUtil.e(TAG, "addAdress select position :"+position+"   selected :"+selected);
		if(selected){
//			String area_id=districtlist.get(position).getArea_id();
//			spinInitData(area_id, spin_street, streetlist);
		}
	}
	//街道
	@ItemSelect({R.id.spin_street})
	public void onStreetSelect(boolean selected, int position){
		LogUtil.e(TAG, "addAdress select position :"+position+"   selected :"+selected);
		
	}
	/*******************************spinner 选择监听事件   end********************************/
	
	
	/**
 	 * 配置Spinner
 	 * @param area_id
 	 * @param spin
 	 */
	private void spinInitData(final String area_id,final Spinner spin,final List<Area> arealist){
 		//发送HTTP请求获取区域信息  不填写area_id 返回省一级  填写area_id返回下一级
		AsyncHttpUtil.get(URLConfigs.AreasInfo_areaid+area_id, null, new TextHttpResponseHandler() {
			@Override
			public void onSuccess(int status, Header[] arg1, String json) {
				if(status==200){
					LogUtil.e(TAG, "add_address json :"+json);
					AreasEntity entity=GsonUtil.GetFromJson(json, AreasEntity.class);
					if(entity.isStatus()){
						//清空arealist数据
						arealist.clear();
						//填充数据
						arealist.addAll(entity.getList());
						//获取所有area_name数据
						List<String> list=new ArrayList<String>();
						for(Area area:arealist){
							list.add(area.getArea_name());
						}
						 //适配器
						  spin_adapter = new ArrayAdapter<String>(ChooseRegionActivity.this,android.R.layout.simple_spinner_item,list);
						//设置下拉列表的风格
						  spin_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
						//将adapter添加到spinner中
						  spin.setAdapter(spin_adapter);
					}else{
						ToastUtils.show(context, R.string.network_unavailable);
					}
				}
			}
			
			@Override
			public void onFailure(int status, Header[] arg1, String json, Throwable arg3) {
				ToastUtils.show(context, R.string.network_unavailable);
			}
		});
	}
}
